package com.hxq.library.controller;

import com.hxq.library.service.IUserService;
import com.hxq.library.service.Impl.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Objects;

/**
 * @Author: Ljx
 * @Date: 2021/11/25 19:43
 * @role:
 */
@WebServlet("/inputNumber")
public class InputNumber extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html,utf-8");
        HttpSession session = req.getSession();
        String account = (String) session.getAttribute("account1");
        IUserService service = new UserService();
        String num1 = req.getParameter("num1");
        double random = Math.random();
        String code = String.valueOf((int) (random*1000000));
        session.setAttribute("code",code);
        if (Objects.equals(num1, "")){
            req.setAttribute("msg4","手机号不能为空！！！");
            req.getRequestDispatcher("inputNum.jsp").forward(req,resp);
        }
        try {
            boolean forgetPassword = service.forgetPassword(account, num1, code);
            if (forgetPassword){
                req.setAttribute("msg3","您的验证码为："+code);
                req.getRequestDispatcher("changePassword.jsp").forward(req,resp);
            }else {
                req.setAttribute("msg4","输入手机号和注册该账号时输入不相同");
                req.getRequestDispatcher("inputNum.jsp").forward(req,resp);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
